package FlowGram.Application;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import FlowGram.Application.IDEWindow.MainWindow;
import FlowGram.Application.IDEWindow.FileManager.ProjectFileItem;
import FlowGram.Application.Parser.Actions.Control.FunctionDefinition;
import FlowGram.Application.Parser.Controllers.FinchController;
import FlowGram.Application.Parser.Controllers.FinchRobotController;
import FlowGram.Application.Parser.Utility.Configuration;
import FlowGram.Application.SimulatorWindow.SimulationWindow;
import FlowGram.Application.User.Login;
import FlowGram.Application.User.User;
import FlowGram.Application.User.UserManager;


/**
 * 
 * @author thackray
 *
 */
public class Application {

	private static Application instance;
	
	protected User currentUser;
	protected MainWindow mainWin;
	protected SimulationWindow simWin;
	protected boolean isFinConnected;
	protected FinchRobotController finchController;
	
	protected String currWorkspace;
	
	
	protected Application(){
		UserManager userman = new UserManager();
		

		//this.currentUser = this.showLoginWindow();
		//this.showIDEWindow();
		//this.mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		System.out.println(this.getWorkspace());
		Configuration.setDefault("config.xml");
		
	}
	public static Application getInstance(){
		if(Application.instance == null){
			Application.instance = new Application();
		}
		return Application.instance;
	}
	public User getCurrentUser(){
		return this.currentUser;
	}
	public void saveCurrentUser(){
		
	}
	public void showIDEWindow(){
		if(this.mainWin == null){
			this.mainWin = new MainWindow();
		}
		this.mainWin.setVisible(true);
	}
	public MainWindow getMainWindow(){
		if(this.mainWin == null){
			this.mainWin = new MainWindow();
		}
		return this.mainWin;
	}
	public void showSimulationWindow(){
		if(this.simWin == null){
			this.simWin = new SimulationWindow();
		}
		this.simWin.setVisible(true);
	}
	public SimulationWindow getSimulationWindow(){
		if(this.simWin == null){
			this.simWin = new SimulationWindow();
		}
		return this.simWin;
	}
	public boolean isFinchConnected(){
		return this.isFinConnected;
	}
	public FinchController getFinchController(){
		return this.finchController;
	}
	public boolean connectFinch(){
		try {
			this.finchController = new FinchRobotController();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		if(this.finchController != null){
			this.isFinConnected = true;
			return true;
		}
		return false;
	}
	public void disconnectFinch(){
		this.isFinConnected = false;
		this.finchController.destruct();
		this.finchController = null;
	}
	public String getDefaultTerrainLocation(){
		String currentDir = new File("").getAbsolutePath();
		String defaultterrainfolder = currentDir + File.separator+"bin"+File.separator+"FlowGram"+File.separator+"Application"+File.separator+"SimulatorWindow"+File.separator+"TerrainsDefault";
		return defaultterrainfolder;
	}
	public String getWorkspace(){
		System.out.println("getWorkspace");
		ArrayList<String> places = new ArrayList<String>();
		places.add("C:\\Users\\Josh\\Desktop\\finchworkspace");
		places.add("C:\\Documents and Settings\\thackray\\Desktop\\finchworkspace");
		places.add("/ilab/users/thackray/Desktop/finchworkspace/");
		places.add("C:\\finchfolder");
		places.add("C:\\java2\\FinchParser\\user\\opena");
		places.add("C:\\Users\\rushamin\\Desktop\\FinchWorkSpace");
		places.add("C:\\Users\\Eric Tsai\\Documents\\C\\Users\\Eric Tsai\\Documents\\2011 - 2012\\cs413\\FinchWorkspace");
		//places.add(this.currentUser.getWorkspace());
	
		for(int i=0; i < places.size(); i++){
			File f = new File(places.get(i));
			System.out.println("does " + f.getAbsolutePath()+ " exist?");
			if(f.exists()){
				System.out.println("found!");
				System.out.println(f.getPath());
				return f.getPath();
			}
		}
		return "";
	}
	public String getCurrentProjectPath(){
		return this.getMainWindow().getCurrentProject();
	}
	public void exit(){
		if(this.mainWin != null){ 
			this.mainWin.shutdown();
			this.mainWin.dispose(); 
		} 
		this.mainWin = null; 
		if(this.simWin != null){ 
			this.simWin.dispose(); 
		} 
		this.simWin = null; 
		/*UserManager uMan = new UserManager(); 
		uMan.update(this.currentUser); 
		this.currentUser = null;*/
		System.exit(0);
	}
	protected User showLoginWindow(){
		while(true){ 
			//Login.createAndShowGUI();
		}
	}
	
	public static Icon loadIcon(String path){
		java.net.URL imageURL = Application.class.getResource("/toolbarButtonGraphics/"+path);
		if (imageURL != null) {
		    ImageIcon icon = new ImageIcon(imageURL);
		    return icon;
		}
		return null;
	}
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		Application app = Application.getInstance();
		app.showIDEWindow();
		app.mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}
